package com.exam.demo.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.exam.demo.common.Result;
import com.exam.demo.entity.Major;
import com.exam.demo.service.MajorService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.io.Serializable;
import java.util.Arrays;
import java.util.List;

/**
 * 功能描述
 *
 * @author yxj30
 * @date 2022/03/16  19:18
 */
@RestController
@RequestMapping("/major")
public class MajorController {
    @Resource
    MajorService majorService;

    //查找页面数据
    @GetMapping
    public Result findPage(@RequestParam Integer pageNum,
                           @RequestParam Integer pageSize,
                           @RequestParam(defaultValue = "") String majorName){

        QueryWrapper<Major> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("major_name",majorName);
        Page<Major> page = majorService.page(new Page<>(pageNum, pageSize), queryWrapper);
        return Result.success(page);
    }
    //查找根据id全部专业数据
    @GetMapping("/selectByCollegeId/{college_id}")
    public Result findPage(@PathVariable String college_id){
        QueryWrapper<Major> queryWrapper = new QueryWrapper<>();
        List<Major> list = majorService.list(queryWrapper.eq("college_id",college_id));
        return Result.success(list);
    }


    //更改和插入
    @PostMapping("/save")
    public Result insert(@RequestBody Major major){
        return Result.success(majorService.saveOrUpdate(major));
    }

    //删除
    @DeleteMapping("/delete/{id}")
    public Result delete(@PathVariable Integer id){
        return Result.success(majorService.removeById(id));
    }


}
